from django.db import models
from apps.goods.models import Goods
from apps.user.models import User, UserAddress

class Order(models.Model):
    order_id = models.AutoField(primary_key=True, unique=True, verbose_name="订单ID")
    # user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    email = models.EmailField(max_length=255, null=False, verbose_name='用户')
    trade_no = models.CharField(max_length=255, unique=True, null=False, verbose_name="订单号")
    pay_status = models.IntegerField(default=0, choices=[(0, '未支付'), (1, '已支付')], verbose_name="订单状态")
    total_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单总金额")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='订单创建时间')
    address = models.IntegerField(max_length=255, null=True, verbose_name="订单地址")
    pay_time = models.DateTimeField(null=True, verbose_name="支付时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name='订单更新时间')
    is_delete = models.BooleanField(default=False, verbose_name="是否删除")

    class Meta:
        db_table = 'order'

class OrderDetail(models.Model):
    order_detail_id = models.AutoField(primary_key=True, unique=True, verbose_name="订单详情ID")
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='detail', verbose_name='订单')
    trade_no = models.CharField(max_length=255, unique=True, null=False, verbose_name="订单号")
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')
    num = models.IntegerField(default=0, verbose_name="商品数量")

    def __str__(self):
        return f'{self.goods.name}'